"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
async function chunkedStringify(data, chunkSize = 1000) {
    if (data === null || data === undefined) {
        return JSON.stringify(data);
    }
    if (typeof data !== 'object') {
        return JSON.stringify(data);
    }
    if (Array.isArray(data)) {
        let result = '[';
        for (let i = 0; i < data.length; i++) {
            const chunk = await chunkedStringify(data[i], chunkSize);
            result += (i === 0 ? '' : ',') + chunk;
            if (i % chunkSize === 0) {
                await new Promise(resolve => setImmediate(resolve));
            }
        }
        return result + ']';
    }
    const keys = Object.keys(data);
    let result = '{';
    for (let i = 0; i < keys.length; i++) {
        const key = keys[i];
        const value = data[key];
        const serializedValue = await chunkedStringify(value, chunkSize);
        result += (i === 0 ? '' : ',') + `"${key}":${serializedValue}`;
        if (i % chunkSize === 0) {
            await new Promise(resolve => setImmediate(resolve));
        }
    }
    return result + '}';
}
exports.default = chunkedStringify;
